<!--
 * @Author: your name
 * @Date: 2022-01-15 14:46:23
 * @LastEditTime: 2022-01-16 18:57:40
 * @LastEditors: Please set LastEditors
 * @Description: 商品列表页面
 * @FilePath: \uniapp\src\pages\goods_list\index.vue
-->
<template>
  <view>
    <searchInput></searchInput>

    <!-- 2 tabs -->
    <u-tabs
      active-color="#eb4450"
      :bar-width="130"
      :list="list"
      :is-scroll="false"
      :current="current"
      @change="change"
    ></u-tabs>

    <!-- 3 tabs内容 条件渲染 -->
    <view class="tab-content">
      <view v-for="(item, index) in list" :key="item.name" class="tabs-item">
        <block v-if="current === index">
          <navigator
            v-for="item1 in goodsList"
            :key="item1.goods_id"
            class="goods-item u-p-10"
            :url="'/pages/goods_detail/index?goods_id=' + item1.goods_id"
          >
            <u-row>
              <u-col span="4">
                <view class="u-flex u-row-center">
                  <u-image
                    width="191rpx"
                    height="191rpx"
                    :src="item1.goods_small_logo"
                  ></u-image>
                </view>
              </u-col>
              <u-col span="8">
                <view style="height: 191rpx" class="u-flex-col u-row-around">
                  <view class="goods-name">{{ item1.goods_name }}</view>
                  <view class="goods-price">￥{{ item1.goods_price }}</view>
                </view>
              </u-col>
            </u-row>
            <u-line class="u-p-10" color="#eee" />
          </navigator>
        </block>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      list: [
        {
          name: "综合",
        },
        {
          name: "销量",
        },
        {
          name: "价格",
        },
      ],
      current: 0,

      // 商品列表
      goodsList: [],
    };
  },
  onLoad({ cid }) {
    // 定义全局变量  存放接口参数
    this.params = {
      // 否	string	关键字
      query: "",
      // 否	string	分类id
      cid: cid || 6,
      // 否	number	页码
      pagenum: 1,
      // 否	number	页容量
      pagesize: 10,
    };

    // 定义 总条数
    this.total = 0;

    // 获取商品列表
    this.getGoodsList();
  },
  // 滚动条触底事件   上拉加载下一页事件
  onReachBottom() {
    // 拿到 已经加载过的数组长度
    // this.goodsList.length
    // 总条数
    if (this.goodsList.length >= this.total) {
      // 没有数据了
      uni.showToast({
        title: "没有更多数据了~",
        icon: "none",
      });
    } else {
      this.params.pagenum++;
      this.getGoodsList();
    }
  },
  // 页面下拉刷新
  async onPullDownRefresh() {
    this.goodsList = [];
    this.params.pagenum = 1;
    await this.getGoodsList();
    uni.stopPullDownRefresh();
  },
  methods: {
    change(index) {
      this.current = index;
    },

    // 获取商品列表数据
    async getGoodsList() {
      const res = await this.$u.get("/goods/search", this.params);

      // 数组合并
      this.goodsList = [...this.goodsList, ...res.goods];
      // 总条数赋值
      this.total = res.total;
    },
  },
};
</script>

<style lang="scss">
.goods-name {
  color: #666;
}
.goods-price {
  color: $themeColor;
}
</style>